package y2016.date1019.pm.拆分重复剔除;

import org.junit.jupiter.api.Test;

import java.io.*;

public class SplitVal {
    public static String[] split(String str, String regex) {
        return str.split(regex);
    }

    /**
     * 重复替换为空
     */
    public static String val(String str, String regex) {
        String[] strings = split(str, regex);
        for (int i = 0; i < strings.length; i++) {
            String str_1 = strings[i];
            for (int j = i + 1; j < strings.length; j++) {
                String str_2 = strings[j];
                if (str_2.equals(str_1)) {
                    strings[j] = "";
                }
            }
            strings[i] += ";";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strings.length; i++) {
            String string = strings[i];
            sb.append(string);
        }
        return sb.toString();
    }

    public static String readFile(String path) {
        File file = new File(path);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            InputStream is = new FileInputStream(file);
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = is.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            bos.flush();
            bos.close();
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return bos.toString();
    }

    @Test
    public void main() throws Exception {
        String str = readFile("C:/Users/LuTao/Desktop/wer/test.txt");
        System.out.println(val(str, ";"));
    }

}

